﻿<?php    
require_once("class.phpmailer.php");
require_once("qq.class.php");
class Sender{
	
	static public function sendMail($sendto_email,$subject,$body,$sender_name,$file_name){
		$maxAddressSize=15;
		$mail = new PHPMailer();
		$error=null;
		//$mail->SMTPDebug  = 2; 
        $mail->IsSMTP();                  // send via SMTP    
		//使用QQ邮箱
        $mail->Host = "smtp.qq.com";   // SMTP servers    
        $mail->SMTPAuth = true;           // turn on SMTP authentication    
        $mail->Username = Conf::get( 'sysMailAccount' );     // SMTP username  注意：普通邮件认证不需要加 @域名    
        $mail->Password = Conf::get( 'pwdMail' );// SMTP password    
        $mail->From = Conf::get( 'sysMailAccount' );      // 发件人邮箱   
        $mail->FromName = "=?utf-8?B?" . base64_encode("$sender_name") . "?=";  // 发件人ID
        $mail->CharSet = "UTF-8";   // 这里指定字符集！    
        $mail->Encoding = "base64";
		//添加附件
		if($file_name!=null&&$file_name!=""){
			
			$temp= explode("\\",$file_name);
			$fileName=$temp[count($temp)-1];
			$fileName=iconv("UTF-8","gbk",$fileName);
			$mail->AddAttachment( $file_name ,$fileName);
		}
		//邮件主题
        $mail->Subject ="=?utf-8?B?" . base64_encode("$subject") . "?="; 
        // 邮件内容    
        //$mail->Body = "=?utf-8?B?" . base64_encode("$body") . "?=";
		$mail->Body=$body; 
		$address=explode(";",$sendto_email);//分割出所有地址
		$address=array_filter($address);
		$addressSize=count($address);//计算所有地址的数量
		if($addressSize>$maxAddressSize){
			$times=floor($addressSize/$maxAddressSize);
			for($i=0;$i<=$times-1;$i++){
				for($j=0;$j<$maxAddressSize;$j++){
					if (filter_var($address[$j+$i*$maxAddressSize], FILTER_VALIDATE_EMAIL)) {
						$mail->AddAddress($address[$j+$i*$maxAddressSize],null);
					}
				}
				if(!$mail->Send()){
						$error.=$mail->ErrorInfo;
					}else{
						$mail->ClearAddresses();
					}
			}
			if($times*$maxAddressSize<$addressSize){
				for($i=$times*$maxAddressSize;$i<$addressSize;$i++){
					if (filter_var($address[$i], FILTER_VALIDATE_EMAIL)) {//验证地址是否合法
					$mail->AddAddress($address[$i],null);
					}			
				}
				if(!$mail->Send()){
					$error.=$mail->ErrorInfo.'2';
				}
			}
			
			if($error!=null){
				echo $error;
			}else{
				echo 1;
			}
		}else{
			for($j=0;$j<$addressSize;$j++){
				if (filter_var($address[$j], FILTER_VALIDATE_EMAIL)) {
					$mail->AddAddress($address[$j],null);
				}
			}		
			if(!$mail->Send())    
			{    
				echo $mail->ErrorInfo;
				exit;    
			}    
			else {    
				echo 1;    
			}    
		}	
	}
	
	static public function sendQQMsg($send_to,$message){
		$receiver=explode(";",$send_to);//分割
		$receiver=array_filter($receiver);//除空
		
		$sysAcc=Conf::get( 'sysQQAccount' );
		$sysPwd=Conf::get( 'pwdQQ' );
		$qq=new qq();
		$sid=$qq->login($sysAcc,$sysPwd);
		foreach($receiver as $re){
			$qq->sendMsg($re,$message,$sid);
		}
	
	}
}

/*
	function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $sender_name,$file_name){
		ChromePhp::log("send");
		global $uploadDir;
		$mail = new PHPMailer();
		//$mail->SMTPDebug  = 2; 
        $mail->IsSMTP();                  // send via SMTP    
		//使用扣扣邮箱
        $mail->Host = "smtp.qq.com";   // SMTP servers    
        $mail->SMTPAuth = true;           // turn on SMTP authentication    
        $mail->Username = "604267811@qq.com";     // SMTP username  注意：普通邮件认证不需要加 @域名    
        $mail->Password = "bin86972793"; // SMTP password    
        $mail->From = "604267811@qq.com";      // 发件人邮箱   
        $mail->FromName = "=?utf-8?B?" . base64_encode("$sender_name") . "?=";  // 发件人ID
        $mail->CharSet = "UTF-8";   // 这里指定字符集！    
        $mail->Encoding = "base64";    
        $mail->AddAddress($sendto_email,null);  // 收件人邮箱和姓名    		
		//发送附件
		if($file_name!=null&&$file_name!=""){
			$mail->AddAttachment( $uploadDir.'/'.$file_name ,$file_name);
		}
		//邮件主题
        $mail->Subject ="=?utf-8?B?" . base64_encode("$subject") . "?="; 
        // 邮件内容    
        $mail->Body = $body;                                                                         
        $mail->AltBody ="text/html";    
        if(!$mail->Send())    
        {    
            echo "邮件发送有误 <p>";    
            echo "邮件错误信息: " . $mail->ErrorInfo;
            exit;    
        }    
        else {    
            echo 1;    
        }    
    }    
   */
	
?>  
